﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Framework.Data.Security;

namespace Framework.Data.Core
{
    public class EntitySystemField : EntityField, IEntitySystemField
    {

        #region "Enums"

        /// <summary>
        /// Enumérateur de champs système
        /// </summary>
        /// <remarks></remarks>
        public enum SystemField
        {
            USER_CREATION,
            USER_MAINTENANCE,
            DATE_CREATION,
            DATE_MAINTENANCE
        }
        #endregion

        #region "Membres"

        //Type du champ système

        protected SystemField _systemFieldType;
        #endregion

        #region "Constructeurs"

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="entity">Entité auquel ce champ appartient</param>
        /// <param name="systemFieldType">Type du champ</param>
        /// <param name="tableName">Nom de la table</param>
        /// <param name="databaseFieldName">Nom du champ dans la bd</param>
        /// <remarks></remarks>
        public EntitySystemField(IBaseEntity entity, SystemField systemFieldType, string tableName, string databaseFieldName)
            : this(entity, systemFieldType, tableName, databaseFieldName, false)
        {
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="entity">Entité auquel ce champ appartient</param>
        /// <param name="systemFieldType">Type du champ</param>
        /// <param name="tableName">Nom de la table</param>
        /// <param name="databaseFieldName">Nom du champ dans la bd</param>
        /// <param name="displayFieldInList">Indique si le champ doit apparaître dans les listes</param>
        /// <remarks></remarks>
        public EntitySystemField(IBaseEntity entity, SystemField systemFieldType, string tableName, string databaseFieldName, bool displayFieldInList)
            : this(entity, systemFieldType, tableName, databaseFieldName, displayFieldInList, tableName + "." + databaseFieldName)
        {
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="entity">Entité auquel ce champ appartient</param>
        /// <param name="systemFieldType">Type du champ</param>
        /// <param name="tableName">Nom de la table</param>
        /// <param name="databaseFieldName">Nom du champ dans la bd</param>
        /// <param name="displayFieldInList">Indique si le champ doit apparaître dans les listes</param>
        /// <param name="listColumnName">Nom de la colonne</param>
        /// <remarks></remarks>
        public EntitySystemField(IBaseEntity entity, SystemField systemFieldType, string tableName, string databaseFieldName, bool displayFieldInList, string listColumnName)
            : base(entity, typeof(DateTime), tableName.ToUpper(), databaseFieldName.ToUpper(), -1, false, true, displayFieldInList, false, listColumnName.ToUpper())
        {

            //On conserve le type du champ système
            this._systemFieldType = systemFieldType;
            //On update les propriétés
            switch (systemFieldType)
            {
                case SystemField.DATE_CREATION:
                case SystemField.DATE_MAINTENANCE:
                    this._fieldValueType = typeof(DateTime);
                    this._fieldSize = -1;
                    break;
                case SystemField.USER_CREATION:
                case SystemField.USER_MAINTENANCE:
                    this._fieldValueType = typeof(string);
                    this._fieldSize = 30;
                    break;
            }
        }

        #endregion

        #region "IEntitySystemField"
        /// <summary>
        /// Procédure qui update le champ système avant la sauvegarde
        /// </summary>
        /// <remarks></remarks>
        public void BeforeSave()
        {
            this.BeforeSave(SecurityManager.Principal.Identity.Name.ToUpper());
        }

        /// <summary>
        /// Procédure qui update le champ système avant la sauvegarde
        /// </summary>
        /// <remarks></remarks>
        public void BeforeSave(string userName)
        {
            switch (this._systemFieldType)
            {
                case SystemField.DATE_MAINTENANCE:
                    this._value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                    break;
                case SystemField.USER_MAINTENANCE:
                    this._value = userName;
                    break;
                case SystemField.DATE_CREATION:
                    if ((this._value == null) || (this.Value.ToString() == new DateTime(0).ToString()))
                    {
                        this._value = DateTime.Now;
                    }
                    break;
                case SystemField.USER_CREATION:
                    if ((this._value == null) || (this.Value.ToString().Length == 0))
                    {
                        this._value = userName;
                    }

                    break;
                default:
                    break;
                //Fait rien
            }
        }

        #endregion

        #region "IEntityField"
        

        string IEntityField.Schema
        {
            get
            {
                return this.Schema;
            }
            set
            {
                this.Schema = value;
            }
        }

        string IEntityField.TableName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IEntityField.DatabaseFieldName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        int IEntityField.FieldSize
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        bool IEntityField.IsPrimaryKey
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        short IEntityField.NumberPrecision
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion

        #region "IField"
        string IField.Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        Type IField.FieldValueType
        {
            get { throw new NotImplementedException(); }
        }

        string IField.FormatString
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        IFormatProvider IField.FormatProvider
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        object IField.Value
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        object IField.OriginalValue
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        bool IField.IsNull
        {
            get { throw new NotImplementedException(); }
        }

        bool IField.IsDirty
        {
            get { throw new NotImplementedException(); }
        }

        IBaseEntity IField.Entity
        {
            get { throw new NotImplementedException(); }
        }

        void IField.AcceptChanges()
        {
            throw new NotImplementedException();
        }

        void IField.RejectChanges()
        {
            throw new NotImplementedException();
        }

        string IField.ToString()
        {
            throw new NotImplementedException();
        }

        void IField.InitializeValue(object value)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}